﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace SocketSend
{
    class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                IPAddress[] ipAddress = Dns.GetHostAddresses("localhost");
                IPEndPoint ipEnd = new IPEndPoint(ipAddress[1], 5658);
                Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);

                s.Connect(ipEnd);
                
                int count = 0;
                while (count < 50)
                {
                    Console.WriteLine("File {0} sending...", count);
                    byte[] fileData =
                        File.ReadAllBytes(args[0]);

                    s.SendFile(args[0], new byte[0], new byte[0], TransmitFileOptions.UseSystemThread);
                    Console.WriteLine("File {0} sent.", count);
                }

                s.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.Read();
        }

        private static void SendFileCallback(IAsyncResult ar)
        {
            // Retrieve the socket from the async state object.
            Socket handler = (Socket)ar.AsyncState;
            try
            {
                // Complete sending the data to the remote device.
                int bytesSent = handler.EndSend(ar);
            }
            catch (Exception e)
            {
            }

        }
    }
}
